package com.imooc.controller;

import com.imooc.utils.RedisOperator;
import net.bytebuddy.agent.builder.AgentBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName HelloController
 * @Description TODO
 * @Author xuhang1
 * @Data 2020/4/27 14:03
 * @Version 1.0
 **/
@ApiIgnore
@RestController
@RequestMapping("redis")
public class RedisController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisOperator redisOperator;

    @GetMapping("/set")
    public Object set(String key,String value){
        redisOperator.set(key,value);
        return "OK";
    }

    @GetMapping("/get")
    public Object get(String key){
        return redisOperator.get(key);
    }

    @GetMapping("/delete")
    public Object delete(String key){
        redisOperator.del(key);
        return "OK";
    }

    @GetMapping("/getALot")
    public Object getALot(String... keys){
        List<String> result = new ArrayList<>();
        for(String k:keys){
            result.add(redisOperator.get(k));
        }
        return result;
    }

    @GetMapping("/mget")
    public Object mget(String... keys){
        List<String> keysList = Arrays.asList(keys);
        return redisOperator.mget(keysList);
    }
}
